﻿using UnityEngine;
using System.Collections;
using System.IO;

public class Box : MonoBehaviour {
	public bool IsDir;
	public string StrPath;
	public string FileText;
	public Texture2D DefaultFile;
	public Texture2D DefaultFolder;
	public Texture2D Current;

	public bool RenderText;
	public bool IsLoading;
	public bool HasImage;
	public bool Reload;
	public bool Resize;

	public bool Contract;
	public bool ContractResize;

	public bool Expand;
	public bool Expanded;
	public bool ExpandFirst;

	public float LargeScale;

	public float ScaleTime;
	public float ScaleTotal;

	public GameObject RenderPlane;
	public Vector3 OldPos;

	public float RealWidth;
	public float RealHeight;



	void Start(){
		Current = new Texture2D(16,16); //(4, 4, TextureFormat.DXT5, false);
		Resize = true;
		ExpandFirst = true;
		//Camera.main.gameObject.GetComponent<render>().ParseExtOnce(StrPath,this.gameObject);

	}
	void OnGUI(){
		if(RenderText){
			FileText = GUI.TextArea(new Rect(0,20,Screen.width-20,Screen.height-40),FileText);
			GUI.Box(new Rect(0,0,Screen.width,20),StrPath);
			if(GUI.Button(new Rect(0,0,128,20),"Save")){
				File.WriteAllText(StrPath,FileText);
			}
			if(GUI.Button(new Rect(Screen.width - 20,0,20,20),"X")){
				RenderText = false;
			}
		}
	}
	void Update(){
		if(Reload){
			HasImage = false;
			Resize = true;
			StartCoroutine(LoadImage());
			Reload = false;
		}
		if(Expand){
			if(ExpandFirst){
				OldPos = RenderPlane.transform.position;
				ScaleTotal = 0;
				ExpandFirst = false;
			}
			ScaleTotal += Time.deltaTime*5;
			if(ScaleTotal >= 4){
				print(new Vector3(1+LargeScale*(RealWidth / RealHeight),1+LargeScale,1+LargeScale).ToString());
				RenderPlane.transform.localScale = new Vector3(1+LargeScale*(RealWidth / RealHeight),1+LargeScale,1+LargeScale);
				ExpandFirst = true;
				Expand = false;
				Expanded = true;
			}

			ScaleTime = ScaleTotal/4;

			RenderPlane.transform.position = new Vector3(OldPos.x,ScaleTime+0.25f,OldPos.z);//Vector3.Lerp(OldPos , OldPos + new Vector3(0,0,0) , ScaleTime);
			RenderPlane.transform.localScale = new Vector3((1+LargeScale*ScaleTime)*(RealWidth / RealHeight),1+LargeScale*ScaleTime,1+LargeScale*ScaleTime);
		}
		//run the same thing in reverse
		if(Contract){
			if(ExpandFirst){
				ScaleTotal = 4;
				ExpandFirst = false;
			}
			if(ContractResize){
				TextureScale.Bilinear(Current,128,128);
				ContractResize = false;
			}
			ScaleTotal -= Time.deltaTime*5;
			if(ScaleTotal <= 0){
				print(new Vector3(1+LargeScale*(RealWidth / RealHeight),1+LargeScale,1+LargeScale).ToString());
				RenderPlane.transform.localScale = new Vector3((RealWidth / RealHeight),1,1);
				RenderPlane.transform.position = OldPos;
				ExpandFirst = true;
				Contract = false;
				Expanded = false;
			}
			ScaleTime = ScaleTotal/4;
			
			RenderPlane.transform.position = new Vector3(OldPos.x,ScaleTime+0.25f,OldPos.z);
			RenderPlane.transform.localScale = new Vector3((1+LargeScale*ScaleTime)*(RealWidth / RealHeight),1+LargeScale*ScaleTime,1+LargeScale*ScaleTime);
		}
	}

	public void Unload(){
		IsLoading = true;
		RenderText = false;
		Resize = true;
		print("Unloading:"+this.gameObject.name);
		if(Expanded){
			Contract = true;
			ContractResize = false;
		}
		HasImage = false;
		if(IsDir){
			ChangeView(DefaultFolder);
		}
		else{
			ChangeView(DefaultFile);
		}
		//yo garbage collect get off your ass and clean some shit
		System.GC.Collect();

		IsLoading = false;
	}

	public IEnumerator LoadImage() {
		IsLoading = true;
		WWW www = new WWW("file://"+StrPath);
		yield return www;

		RealWidth = www.texture.width;
		RealHeight = www.texture.height;
		Texture2D TempTex = new Texture2D(4,4);
		www.LoadImageIntoTexture(TempTex);
		if(Resize){
			TextureScale.Bilinear(TempTex,128,128);
			Resize = false;
		}
		ChangeView(TempTex);
		IsLoading = false;
		HasImage = true;
	}
	public void ChangeView(Texture2D NewTex){

		this.transform.GetChild(1).GetChild(0).renderer.material.mainTexture = NewTex;
		this.transform.GetChild(1).GetChild(0).renderer.material.color = Color.white;
		Current = NewTex;
	}

}
